/*
 * @Author: your name
 * @Date: 2020-06-22 16:13:14
 * @LastEditTime: 2020-06-22 17:28:15
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \DepView\src\main\java\me\nettee\depview\main\HttpServer.java
 */ 
package me.nettee.depview.main;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

import org.apache.commons.lang3.StringUtils;

/**
 * @ClassName: HttpServer
 * @Description:  服务端
 * @Author: liuhefei
 * @Date: 2019/6/23
 * @blog: https://www.imooc.com/u/1323320/articles
 **/
public class HttpServer {
    public static void main(String[] args) throws IOException {
        if (args.length < 1) {
            throw new IllegalArgumentException("No config file specified");
        }
        String filename = args[0];

        File testSubjectConfigFile = new File(filename);

        Config conf = ConfigFactory.parseFile(testSubjectConfigFile);
        Config config = conf.getConfig("testSubject");

        TestSubject testSubject = TestSubject.fromConfig(config);
        String safeName = StringUtils.replaceAll(testSubject.getName(), "[ \\s/\\\\~`?*^&$#@%]", "-");
        String webroot="output\\"+safeName+"\\";
        //启动服务器，监听9005端口
        
        ServerSocket server = new ServerSocket(0);
        int port=server.getLocalPort();
        System.out.println("服务器启动，监听"+port+"端口....");
        String url="http://127.0.0.1:"+port+"/";
        Runtime.getRuntime().exec(
					"rundll32 url.dll,FileProtocolHandler " + url);
        while (!Thread.interrupted()){
            //不停接收客户端请求
            Socket client = server.accept();
            //开启线程
            new Thread(new ServerThread(client,webroot)).start();
        }
        server.close();
    }
}